/****************************************************
          File Name:13-4_imply.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 03 Jan 2015 07:57:50 PM CST
*****************************************************/

#include "13-4.h"
#include <cstring>

Port::Port(const char *br, const char *st, int b)
{
    brand = new char[strlen(br) + 1];
    strcpy(brand, br);
    strcpy(style, st);
    bottles = b;
}

Port::Port(const Port & p)
{
    brand = new char[strlen(p.brand) + 1];
    strcpy(brand, p.brand);
    strcpy(style, p.style);
    bottles = p.bottles;
}

Port & Port::operator=(const Port & p)
{
    if(this == &p)
        return *this;

    delete [] brand;
    brand = new char[strlen(p.brand) + 1];
    strcpy(brand, p.brand);
    strcpy(style, p.style);
    bottles = p.bottles;

    return *this;
}

Port & Port::operator+=(int b)
{
    bottles += b;
    return *this;
}

Port & Port::operator-=(int b)
{
    bottles -= b;
    return *this;
}

void Port::show() const
{
    std::cout << "Brand: " << brand << "\n"
        << "Kind: " << style << "\n" 
        << "Bottles: " << bottles << "\n";
}

ostream & operator<<(ostream & os, const Port & p)
{
    os << p.brand << ", " << p.style << ", " << p.bottles;
    return os;
}

// derived class

VintagePort::VintagePort()
    : Port("none", "none")
{
    nickname = new char[strlen("none") + 1];
    strcpy(nickname, "none");
    year = 0;
}

VintagePort::VintagePort(const char *br, int b, const char *nn, int y)
    : Port(br,"vintage" ,b)                                                    // ?
{
    nickname = new char[strlen(nn) + 1];
    strcpy(nickname, nn);
    year = y;
}

VintagePort::VintagePort(const VintagePort & vp)
    : Port(vp)                                                          // force conversion
{
    nickname = new char[strlen(vp.nickname) + 1];
    strcpy(nickname, vp.nickname);
    year = vp.year;
}

VintagePort & VintagePort::operator=(const VintagePort & vp)
{
    if(this == &vp)
        return *this;

    delete nickname;
    nickname = new char[strlen(vp.nickname) + 1];
    strcpy(nickname, vp.nickname);

    Port::operator=(vp);                        // force conversion ?

    return *this;
}

void VintagePort::show() const
{
    Port::show();
    std::cout << "Nickname: " << nickname << "\n"
        << "Year: " << year << "\n";
}

ostream & operator<<(ostream & os, const VintagePort & vp)
{
    os << Port(vp);         // force conversion
    std::cout << ", " << vp.nickname << ", " << vp.year;

    return os;
}
